package com.yiren.dbaa.module.help_center.view.fragment;

import android.view.View;
import android.widget.TextView;

import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.help_center.api.IHardwareUpdate;
import com.yiren.dbaa.module.help_center.api.ISoftUpdate;
import com.yiren.dbaa.module.help_center.api.impl.HardwareUpdateImpl;
import com.yiren.dbaa.module.help_center.api.impl.SoftUpdateImpl;
import com.yiren.dbaa.module.help_center.contract.UpdateContract;
import com.yiren.dbaa.module.help_center.presenter.UpdatePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/7/25 13:56
 * @description: 软件、硬件更新的Fragment
 */
public class UpdateFragment extends LazyLoadFragment implements UpdateContract.View {

    @BindView(R.id.tv_soft_version)
    TextView tvSoftVersion;
    @BindView(R.id.tv_soft_notes)
    TextView tvSoftNotes;
    @BindView(R.id.tv_soft_update)
    TextView tvSoftUpdate;
    @BindView(R.id.tv_latest_soft)
    TextView tvlatestSoft;
    @BindView(R.id.tv_hardware_version)
    TextView tvHardwareVersion;
    @BindView(R.id.tv_hardware_notes)
    TextView tvHardwareNotes;
    @BindView(R.id.tv_hardware_update)
    TextView tvHardwareUpdate;
    @BindView(R.id.tv_latest_hardware)
    TextView tvLatestHardware;

    private ISoftUpdate iSoftUpdate;
    private IHardwareUpdate iHardwareUpdate;
    private UpdateContract.Presenter updatePresenter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_system_update;
    }

    @Override
    protected void init() {
        iSoftUpdate = new SoftUpdateImpl(getActivity());
        iHardwareUpdate = new HardwareUpdateImpl(getActivity());
        updatePresenter = new UpdatePresenter(getActivity());
        updatePresenter.getSoftUpdateInfo(this);
        reHardwareInfo();
    }

    private void reHardwareInfo() {
        updatePresenter.getHardwareUpdateInfo(this);
    }

    @OnClick({R.id.tv_soft_update, R.id.tv_hardware_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_soft_update:
                iSoftUpdate.checkSoftUpdate(true);
                break;
            case R.id.tv_hardware_update:
                iHardwareUpdate.checkSystemUpdate(true, new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        reHardwareInfo();
                    }
                });
                break;
        }
    }

    @Override
    public void showSoftUpdateInfo(String curVersion, String notes, boolean isShowbt) {
        if (isShowbt) {
            tvSoftUpdate.setVisibility(View.VISIBLE);
            tvlatestSoft.setVisibility(View.GONE);
        } else {
            tvSoftUpdate.setVisibility(View.GONE);
            tvlatestSoft.setVisibility(View.VISIBLE);
        }
        tvSoftVersion.setText("当前软件版本号：\r\n" + curVersion);
        tvSoftNotes.setText(notes);
    }

    @Override
    public void showHardwareUpdateInfo(String curVersion, String notes, boolean isShowbt) {
        if (isShowbt) {
            tvHardwareUpdate.setVisibility(View.VISIBLE);
            tvLatestHardware.setVisibility(View.GONE);
        } else {
            tvHardwareUpdate.setVisibility(View.GONE);
            tvLatestHardware.setVisibility(View.VISIBLE);
        }
        tvHardwareVersion.setText("当前硬件版本号：\r\n" + curVersion);
        tvHardwareNotes.setText(notes);
    }
}
